﻿using Microsoft.Extensions.Options;
using RabbitMQ.Client;
using SHKF.B2C.MQBase;
using SHKF.B2C.MQBase.Model;
using SHKF.B2C.MQBase.Model.Order;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHKF.B2C.WebService
{
    public class MQProducers : RabbitMqCore
    {
        public MQProducers(IOptionsMonitor<MQConfig> config) : base(config)
        {

        }
 
        /// <summary>
        /// 通知减库存
        /// </summary> 
        public void SendUpdateStockQty(UpdateStock stock)
        {
            IModel model = connection.CreateModel();

            string queue = base.Update_Stock_Queue;
            string exchange = base.Update_Stock_Exchange;

            //队列 
            model.QueueDeclare(queue: queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
            model.ExchangeDeclare(exchange: exchange, ExchangeType.Fanout, durable: true, autoDelete: false, arguments: null);
            model.QueueBind(queue: queue, exchange: exchange, routingKey: "", arguments: null);

            string msg = System.Text.Json.JsonSerializer.Serialize<UpdateStock>(stock);
            byte[] body = base.CreateBody(msg);
            model.BasicPublish(exchange, "", mandatory: false, basicProperties: null, body);
        }

    }
}
